package com.gy.leetcode;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 给定一个二维平面，平面上有 n 个点，求最多有多少个点在同一条直线上。
 */
public class 直线上最多的点数 {
    public int maxPoints(int[][] points) {
        //xy比例数组
        long[] prop=new long[points.length];
        //横坐标数组
        long[]  xArray=new long[points.length];
        //纵坐标数组
        long[]  yArray=new long[points.length];

        for (int i = 0; i < points.length; i++) {
            prop[i]=points[i][0]/points[i][1];
            xArray[i]=points[i][0];
            yArray[i]=points[i][1];
        }
        int num1=extracted(prop);
        int num2=extracted(xArray);
        int num3=extracted(yArray);




        return Math.max(num1,Math.max(num2,num3));
    }

    private int extracted(long[] prop) {
        Map<Long, Integer> sourceMap = new HashMap<>();
        for (int i = 0; i< prop.length; i++) {
            if (sourceMap.get(prop[i]) == null) {
                sourceMap.put(prop[i], 1);
            } else {
                int temp = sourceMap.get(prop[i]);
                sourceMap.put(prop[i], temp +1);
            }
        }
        return Collections.max(sourceMap.values());

    }
}
